# 使用大模型进行规划
import json

import LLM

ONE_DAY = 360
ONE_HOUR = 30
ONE_MIN = 2


def list_sort(elem):
    return elem.get('times')


def plan_position_time(rider_info: dict, pickup_merchant: list):
    """
    计划第二天前往的地点与时间
    """
    if not pickup_merchant:
        info = {"has_pickup_merchant": False}
    else:
        pickup_merchant.sort(key=list_sort, reverse=True)
        info = {"has_pickup_merchant": True, "pickup_merchant": pickup_merchant}
    info["now_location"] = rider_info["home"]
    info["rider_status"] = rider_info["rider_status"]
    # 1表示是要询问明天的工作地点与时间计划
    response = json.loads(LLM.get_response_from_llm(1, info))
    print(response)
    time_int1 = response.get('go_to_work_time').split(':')
    time_int2 = response.get('get_off_work_time').split(':')
    work_time = int(time_int2[0]) - int(time_int1[0])
    rider_info["up_time"] = rider_info["sleep_time"] + (12 - work_time) * ONE_HOUR
    rider_info["sleep_time"] = rider_info["up_time"] + work_time * ONE_HOUR
    rider_info["start_position"] = response.get('coordinate')
    # print(work_time)


if __name__ == "__main__":
    # test
    pickup_merchant = [{'times': 1, 'location': (26, 92)}, {'times': 10, 'location': (26, 99)},
                       ]
    #pickup_merchant=[]
    info2 = {"pickup_merchant": pickup_merchant}
    d = {"home": (10, 10),"rider_status":'up'}
    plan_position_time(d, pickup_merchant)


def trick_to_time(trick_step):
    """
    360模拟一天，一个trick表示2分钟
    :param trick_step: 时间步
    :return: [hour, min]
    """
    #  360模拟一天，一个trick表示2分钟
    trick_step %= ONE_DAY
    time_hour = int(trick_step / 30)
    time_min = trick_step % 30 * 2
    return [time_hour, time_min]


def time_to_trick(time_step):
    """
    转为trick
    :param time_step: [hour, min]
    :return:
    """
    time_hour = time_step[0]
    time_min = time_step[1]
    trick_step = time_hour * 30 + time_min * 2
    return trick_step


def check_money(rider_info: dict, new_money):
    #  更新金钱获得情况
    if rider_info["one_day_money"] >= new_money and rider_info["dissatisfied"] <= 10:
        rider_info["dissatisfied"] += 1
    elif rider_info["one_day_money"] < new_money:
        rider_info["dissatisfied"] -= 1
    rider_info["money"] += new_money
    rider_info["one_day_money"] = new_money
    for key in rider_info["new_time_money"].keys():
        rider_info["new_time_money"][key] = 0
